package com.jingfang.cloud.widget.main;


import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.util.SystemInfo;
import com.jingfang.cloud.MzContext;
import com.jingfang.cloud.MzPrefs;
import com.jingfang.cloud.constant.MzConstants;
import com.jingfang.cloud.constant.ui.Resources;
import com.jingfang.cloud.core.SerialPortService;
import dorkbox.systemTray.SystemTray;
import lombok.Getter;
import org.dromara.quickswing.ui.app.QSContext;
import org.dromara.quickswing.ui.app.QSWidget;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

@Getter
public class MainWidget extends QSWidget<MzPrefs> {
    private final Object[] options = {"确定", "退出"};
    private final MainComponent mainComponent;
    @Getter
    private final SerialPortService serialPortService;

    public MainWidget(QSContext<? extends QSWidget<MzPrefs>, MzPrefs> context, String title, MzPrefs prefs, SerialPortService serialPortService) throws HeadlessException {
        super(context, title, prefs);
        this.serialPortService = serialPortService;
        if (SystemInfo.isWindows) {
            FlatLaf.setUseNativeWindowDecorations(true);
        }
        if (SystemInfo.isMacFullWindowContentSupported) {
            this.rootPane.putClientProperty("apple.awt.transparentTitleBar", true);
            this.rootPane.putClientProperty("apple.awt.windowTitleVisible", true);
            this.rootPane.putClientProperty(FlatClientProperties.MACOS_WINDOW_BUTTONS_SPACING,
                    FlatClientProperties.MACOS_WINDOW_BUTTONS_SPACING_MEDIUM);
        }
        this.setIconImages(Resources.MZ_WINDOW_ICONS);
        this.setResizable(true);
        this.setSize(350, 550);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                doExit();
            }
        });
        this.mainComponent = new MainComponent(this);
        this.setContentPane(mainComponent.getContentPanel());
        Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
        int margin = 50;
        int x = screenDimension.width - this.getWidth() - margin;
        int y = screenDimension.height - this.getHeight() - margin;
        this.setLocation(x, y);
    }


    @Override
    protected void preMenuBarInit(MzPrefs ddcPrefs, SplashScreen splashScreen) {

    }

    @Override
    public void doExit() {
        int doExit = JOptionPane.showOptionDialog(this, "隐藏界面到托盘？", "提示", JOptionPane.DEFAULT_OPTION,
                JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
        switch (doExit) {
            case 0: {
                this.setVisible(false);
                this.initSystemTray((MzContext) getContext());
                break;
            }
            case 1: {
                super.doExit();
                break;
            }
            default: {
            }
        }
    }

    private void initSystemTray(MzContext context) {
        SystemTray systemTray = getSystemTray(context);
        JMenu systemTrayMenu = new JMenu();
        JMenuItem displayMenuItem = new JMenuItem("显示界面");
        displayMenuItem.addActionListener(e -> {
            this.setVisible(true);
            systemTray.shutdown();
        });

        systemTrayMenu.add(displayMenuItem);
        JMenuItem exitMenuItem = exitMenuItem(context);
        systemTrayMenu.add(new JSeparator());
        systemTrayMenu.add(exitMenuItem);
        systemTray.setMenu(systemTrayMenu);
    }

    private @NotNull JMenuItem exitMenuItem(MzContext context) {
        JMenuItem exitMenuItem = new JMenuItem("退出应用");
        exitMenuItem.addActionListener(e -> {
                    super.doExit();
                }
        );
        return exitMenuItem;
    }

    private static @NotNull SystemTray getSystemTray(MzContext context) {
        SystemTray.DEBUG = true;
        SystemTray systemTray = SystemTray.get(MzConstants.APP_SYSTEM_TRAY);
        systemTray.setImage(Resources.getImageUrl(MzConstants.KEY_LOGO_PNG));

        if (SystemInfo.isWindows) {
            systemTray.setTooltip(
                    MzConstants.APP_NAME
            );
        }
        return systemTray;
    }
}